package com.chenfan.magic.tags;

import com.chenfan.magic.utils.IDKit;
import com.chenfan.magic.utils.StringKit;
import lombok.Data;
import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.nodes.Element;

import java.io.Serializable;

/**
 * @author lm
 * template标签模型
 * <template id="123456"></template>
 */
@Data
public class TagTemplate extends TagAbstract implements Serializable {

    private static final long serialVersionUID = 1L;

    public static String tagNames = "template";

    /**
     * 数据源-引用内建索引
     */
    protected String datasource;
    protected String __datasource;

    /**
     * 执行类型: query, update, 忽略大小写
     */
    protected String method;
    protected String __method;

    /**
     * 结果集类型: row, rows, count, 忽略大小写
     */
    protected String resultType;
    protected String __resultType;

    /**
     * 大数据导出相关
     */
    protected Boolean export;
    protected Long exportStep;
    protected String exportName;
    protected String exportImages;

    public TagTemplate() {
    }

    public TagTemplate(Element templateTag, int index) {
        this.element = templateTag.toString();
        this.id = templateTag.attr("id");
        if (StringKit.isBlank(this.id)) {
            this.id = IDKit.fastUUID() + "";
        }
        this.name = templateTag.attr("name");
        this.datasource = templateTag.attr("datasource");
        String sort = templateTag.attr("sort");
        if (StringKit.isBlank(sort)) {
            this.sort = index - 10000;
        } else {
            this.sort = Integer.parseInt(sort);
        }
        this.method = templateTag.attr("method");
        if (StringKit.isBlank(this.method)) {
            this.method = "query";
        }
        this.resultType = templateTag.attr("result-type");
        if (StringKit.isBlank(this.resultType)) {
            this.resultType = "rows";
        }
        this.resultKey = templateTag.attr("result-key");
        if (StringKit.isBlank(this.resultKey)) {
            this.resultKey = "result_" + this.id;
        }
        String respond = templateTag.attr("respond");
        if (StringKit.isBlank(respond)) {
            this.respond = true;
        } else {
            this.respond = Boolean.valueOf(respond);
        }
        String resolve = templateTag.attr("resolve");
        if (StringKit.isBlank(resolve)) {
            this.resolve = true;
        } else {
            this.resolve = Boolean.valueOf(resolve);
        }
        this.context = StringEscapeUtils.unescapeHtml4(templateTag.text());

        this.__sort = templateTag.attr(":sort");
        this.__respond = templateTag.attr(":respond");
        this.__resolve = templateTag.attr(":resolve");
        this.__resultKey = templateTag.attr(":result-key");
        this.__resultType = templateTag.attr(":result-type");
        this.__datasource = templateTag.attr(":datasource");
        this.__method = templateTag.attr(":method");

        this.engine = templateTag.attr("engine");

        /**
         * 导出相关, 当且仅当method值为query时
         */
        String export = templateTag.attr("export");
        if (StringKit.isBlank(export) || !"query".equalsIgnoreCase(this.method) || !"rows".equalsIgnoreCase(this.resultType)) {
            this.export = false;
        } else {
            this.export = Boolean.valueOf(export);
        }
        String exportStep = templateTag.attr("export-step");
        if (StringKit.isNotBlank(exportStep)) {
            this.exportStep = Long.parseLong(exportStep);
        }

        String exportName = templateTag.attr("export-name");
        if (StringKit.isNotBlank(exportName)) {
            this.exportName = exportName;
        } else {
            this.exportName = "魔方导出数据";
        }

        String exportImages = templateTag.attr("export-images");
        if (StringKit.isNotBlank(exportImages)) {
            this.exportImages = exportImages;
        }

    }

}
